Page({
  data: {
    score: 0,
    level: 1,
    highScore: 0,
    isNewRecord: false
  },

  onLoad(options) {
    const score = parseInt(options.score) || 0
    const level = parseInt(options.level) || 1
    
    this.setData({
      score: score,
      level: level
    })
    
    this.checkHighScore()
  },

  checkHighScore() {
    const currentHighScore = wx.getStorageSync('highScore') || 0
    const isNewRecord = this.data.score > currentHighScore
    
    this.setData({
      highScore: Math.max(currentHighScore, this.data.score),
      isNewRecord: isNewRecord
    })
    
    if (isNewRecord) {
      wx.setStorageSync('highScore', this.data.score)
    }
  },

  playAgain() {
    wx.redirectTo({
      url: '/pages/game/game'
    })
  },

  backToHome() {
    wx.navigateBack({
      delta: 2
    })
  },

  onShareAppMessage() {
    return {
      title: `我在坦克打飞机中获得了${this.data.score}分，到达第${this.data.level}关！`,
      path: '/pages/index/index'
    }
  }
})
